﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ServiceModel;
using System.ServiceModel.Channels;

namespace DevFX.Infrastructure.Commons
{
    public static class WcfServiceClientFactory<TServiceClient, TService>
        where TServiceClient : ClientBase<TService>, TService
        where TService : class
    {
        public static TServiceClient CreateServiceClient()
        {
            var typeName = typeof(TService).Name;
            var serviceAddress = "../" + typeName + ".svc";
            return CreateServiceClient(serviceAddress);
        }

        public static TServiceClient CreateServiceClient(string serviceAddress)
        {
#if SILVERLIGHT
            var endpointAddr = new EndpointAddress(new Uri(Application.Current.Host.Source, serviceAddress));
            var binding = new BasicHttpBinding();
            var ctor = typeof(TServiceClient).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
            return (TServiceClient)ctor.Invoke(new object[] { binding, endpointAddr });
#else
            throw new NotSupportedException();
#endif
        }

        public static TServiceClient CreateServiceClient(Uri serviceAddress)
        {
#if SILVERLIGHT
            var endpointAddr = new EndpointAddress(serviceAddress);
            var binding = new BasicHttpBinding();
            var ctor = typeof(TServiceClient).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
            return (TServiceClient)ctor.Invoke(new object[] { binding, endpointAddr });
#else
            throw new NotSupportedException();
#endif
        }
    }
}
